package com.imm.da.main.test

import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.imm.da.base.vm.BaseViewModel
import com.imm.da.main.weather.api.HeCityService
import com.imm.da.main.weather.api.HeWeatherService
import com.qweather.sdk.bean.geo.GeoBean
import com.qweather.sdk.bean.weather.WeatherNowBean

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 *
 * @author Created by zhangmz
 * @date on 2021/12/3
 */
class MyViewModel : BaseViewModel() {

    private fun queryHotCity(service : HeCityService, context : AppCompatActivity, list : List<GeoBean.LocationBean>) {
        val city = MutableLiveData<List<GeoBean.LocationBean>>()
        request(
            city,
            query = { service.hotCity(city) }
        ).observe(context){
            Log.v("aaaa", "it size: ${it.size}")
            Log.v("aaaa", "list size: ${list.size}")
            for (i in it.indices){
                Log.v("aaaa", "name: ${it[i].name}, country: ${it[i].country}, tz: ${it[i].tz}, fxLink: ${it[i].fxLink}, rank: ${it[i].rank}, type: ${it[i].type}, utcOffset: ${it[i].utcOffset}")
            }
        }
    }

    fun queryCityList(context : AppCompatActivity) {
        val service = HeCityService(context)
        val cityList = MutableLiveData<List<GeoBean.LocationBean>>()
        request(
            cityList,
            query = { service.cityList(cityList, 10, "陕西") }
        ).observe(context){
            queryHotCity(service, context, it)
        }
    }

    fun queryWeather(context : AppCompatActivity){
        val service = HeWeatherService(context)
        val weather = MutableLiveData<WeatherNowBean.NowBaseBean>()
        request(weather){
            service.query("CN101010100", weather)
        }.observe(context){
            Log.v("aaaa", "queryWeather text: ${it.text}, windDir: ${it.windDir}")
        }

    }

}